package bitOperation;

/*
 *@description 二进制求和
 *@author hkfan
 *@version 1.0
 *@create 2025/6/23 16:55
 */
public class LeetCode_67 {
    public String addBinary(String a, String b) {
        if (a.length()<b.length()){
            return addBinary(b,a);
        }
        int i=a.length()-1,j=b.length()-1;
        String res = "";
        Boolean flag =false;
        while (j>=0){
            Integer num1 = Integer.parseInt(String.valueOf(a.charAt(i))) ;
            Integer num2 = Integer.parseInt(String.valueOf(b.charAt(j))) ;
            int sum = num2+num1+((flag)?1:0);
            if (sum>=2){
                flag=true;
                sum-=2;
            }else {
                flag=false;
            }
            res=""+sum+res;
            i--;j--;
        }
        while (i>=0){
            Integer num1 = Integer.parseInt(String.valueOf(a.charAt(i))) ;
            int sum = num1+((flag)?1:0);
            if (sum>=2){
                flag=true;
                sum-=2;
            }else {
                flag=false;
            }
            res=""+sum+res;
            i--;
        }
        return (flag)?"1"+res:res;
    }
}
